<?php
/** 
*
* @package phpBB3
* @version $Id: function_auto_pm.php 21 2007-05-04 01:08:10Z highwayoflife $
* @copyright (c) 2005 Star Trek Guide Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
 * Send a Welcome PM to to a new user on authentication varification.
 * Selects from a variety of drafts defined in the auto_pm table, and randomly chooses a user and draft to use as the welcome PM
 * Utilizes pre-defined BBCodes as well
 * 
 * @param int $user_id User ID of user receiving welcome PM
 * @param string $username Username of user receiving welcome PM
 */
function user_send_welcome_pm($user_id, $username)
{
	global $table_prefix, $db, $user, $config;
	
	if (!$user_id || !$username)
	{
		return false;
	}
	
	// we could put the constant in the constants file, but we are still in Beta, so we define it here...
	if (!defined('AUTO_PM_TABLE'))
	{
		define('AUTO_PM_TABLE', $table_prefix . 'auto_pm');
	}
	
	// grab all the Auto PM’s previously created and available for usage
	$sql = 'SELECT ap.user_id, d.draft_id, d.draft_subject, d.draft_message
			FROM ' . DRAFTS_TABLE . ' d
			INNER JOIN ' . AUTO_PM_TABLE . ' ap
				ON d.draft_id = ap.draft_id';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{	
		// put each draft field in an array, and group for each user
		$draft_ary[$row['user_id']][] = array(
			'user_id'	=> $row['user_id'],
			'subject'	=> $row['draft_subject'],
			'message'	=> $row['draft_message'],
		);
	}
	$db->sql_freeresult($result);
	
	// there were no drafts available... use the ACP defined Auto PM
	if (!sizeof($result))
	{
		$auto_pm = array(
			'user_id'	=> $config['pm_user_id'],
			'subject'	=> $config['pm_subject'],
			'message'	=> $config['pm_message'],
		);
	}
	else
	{
		// randomly pick from the users available
		$first_key = array_rand($draft_ary);
		// randomly pick from the drafts available for the chosen user
		$second_key = array_rand($draft_ary[$first_key]);
		// save this randomly selected draft
		$auto_pm = $draft_ary[$first_key][$second_key];
	}
	
	// grab details about the user we are going to send the PM from
	$sql = 'SELECT user_id, username, user_colour, user_ip
			FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $auto_pm['user_id'];
	$result = $db->sql_query($sql);
	$user_row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	// set some generic pre-BBCode replacements
	// find..
	$pre_bbcodes = array(
		'[username]'	=> get_username_string('username', $user_id, $username),
		'[me]'			=> get_username_string('full', $user_row['user_id'], $user_row['username'], $user_row['user_colour']),
		'[sitename]'	=> $config['sitename'],
		'[site_desc]'	=> $config['site_desc'],
		'[board_rules]'	=> '[url=' . $config['board_rules_url'] . ']' . $config['board_rules_title'] . '[/url]',
		'[board_posts]'	=> $config['num_posts'],
		'[board_topics]'=> $config['num_topics'],
		'[board_users]'	=> $config['num_users'],
	);
	
	// we can use the BBCodes for both the message and the subject.
	$auto_pm['message'] = str_replace(array_keys($pre_bbcodes), array_values($pre_bbcodes), $auto_pm['message']);
	$auto_pm['subject'] = str_replace(array_keys($pre_bbcodes), array_values($pre_bbcodes), $auto_pm['subject']);
	
	// set some variables...
	$uid = $bitfield = $flags = '';
	$allow_bbcode = $allow_urls = $allow_smilies = true;
	
	// generate text for storage into display
	generate_text_for_storage($auto_pm['message'], $uid, $bitfield, $flags, $allow_bbcode, $allow_urls, $allow_smilies);
	
	// ready for output...
	$message = generate_text_for_display($auto_pm['message'], $uid, $bitfield, $flags);
	
	// prepare private message data
	$pm_data = array(
	   'from_user_id'	=> $user_row['user_id'],
	   'from_user_ip'	=> $user_row['user_ip'],
	   'from_username'	=> $user_row['username'],
	   'enable_sig'		=> true,
	   'enable_bbcode'	=> $allow_bbcode,
	   'enable_smilies'	=> $allow_smilies,
	   'enable_urls'	=> $allow_urls,
	   'icon_id'		=> 0,
	   'bbcode_bitfield'=> $bitfield,
	   'bbcode_uid'		=> $uid,
	   'message'		=> $message,
	   'address_list'	=> array('u' => array($user_id => 'to')),
	);
	
	// welcome the user!
 	submit_pm('post', $auto_pm['subject'], $pm_data, false);
}
?>